import os
from pathlib import Path
import urllib.request
import shutil

def create_file(file_path, content):
    """Create a file with the given content"""
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)

def download_file(url, destination):
    """Download a file from a URL to the destination path"""
    os.makedirs(os.path.dirname(destination), exist_ok=True)
    with urllib.request.urlopen(url) as response, open(destination, 'wb') as out_file:
        shutil.copyfileobj(response, out_file)

def setup_android_resources(project_root):
    """Set up all required Android resources"""
    res_dir = os.path.join(project_root, 'app', 'src', 'main', 'res')
    
    # 1. Create raw directory and add alarm sound
    raw_dir = os.path.join(res_dir, 'raw')
    os.makedirs(raw_dir, exist_ok=True)
    
    # Download a default alarm sound (creative commons licensed sound)
    alarm_sound_url = "https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3"
    alarm_sound_url = "https://img.tukuppt.com/newpreview_music/00/06/20/5cc17e19d965568743.mp3"
    download_file(alarm_sound_url, os.path.join(raw_dir, 'alarm_sound.mp3'))
    
    # 2. Create missing drawables
    drawable_dir = os.path.join(res_dir, 'drawable')
    create_file(os.path.join(drawable_dir, 'ic_add.xml'), """<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24"
        android:tint="?attr/colorControlNormal">
        <path android:fillColor="@android:color/white" android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
    </vector>""")
    
    create_file(os.path.join(drawable_dir, 'ic_edit.xml'), """<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24"
        android:tint="?attr/colorControlNormal">
        <path android:fillColor="@android:color/white" android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83l3.75,3.75l1.83,-1.83z"/>
    </vector>""")
    
    create_file(os.path.join(drawable_dir, 'ic_delete.xml'), """<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24"
        android:tint="?attr/colorControlNormal">
        <path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6V19zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
    </vector>""")
    
    # 3. Create other required drawables
    create_file(os.path.join(drawable_dir, 'bg_button.xml'), """<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimary"/>
    <corners android:radius="8dp"/>
</shape>""")
    
    create_file(os.path.join(drawable_dir, 'bg_card.xml'), """<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white"/>
    <corners android:radius="8dp"/>
</shape>""")
    
    # 4. Update colors.xml
    values_dir = os.path.join(res_dir, 'values')
    create_file(os.path.join(values_dir, 'colors.xml'), """<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="background">#F5F5F5</color>
    <color name="alarmBackground">#FFF9C4</color>
    <color name="textPrimary">#212121</color>
    <color name="textSecondary">#757575</color>
    <!-- Material Colors -->
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
</resources>""")
    
    print("✅ All Android resources set up successfully!")

if __name__ == "__main__":
    project_root = input("Enter path to your Android project root: ").strip()
    
    if not os.path.isdir(project_root):
        print(f"Error: {project_root} is not a valid directory")
    elif not os.path.exists(os.path.join(project_root, 'app', 'src', 'main')):
        print("Error: Not an Android project directory")
    else:
        setup_android_resources(project_root)
        print("\nNext steps in Android Studio:")
        print("1. Build -> Clean Project")
        print("2. Build -> Rebuild Project")
        print("3. File -> Invalidate Caches / Restart if needed")